package com.samp.lucene.core;

import java.io.IOException;
import java.nio.file.Paths;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.IntPoint;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.wltea.analyzer.lucene.IKAnalyzer;

public class DeleteDemo {
	
	/**
	 * 1：注意iw.close()的位置，在iw没有close之前，通过iw删除的文档还是可以查询的。
	 * @param indexPath
	 * @throws IOException
	 */
	public static void indexDeletedByWriter(String indexPath) throws IOException{
		IndexSearcherDemo.searchDoc(indexPath, "title","百度", 100);
		Directory directory = FSDirectory.open(Paths.get(indexPath));
		Analyzer analyzer = new IKAnalyzer(true, true, false);
		IndexWriterConfig iwc = new IndexWriterConfig(analyzer);
		//很多文档时，可以设置ram参数。
		//iwc.setRAMBufferSizeMB(128.0);
		IndexWriter iw = new IndexWriter(directory, iwc);
		//iw.deleteDocuments(new Term("id","1"));//id是int类型，用term删除不了 
		iw.deleteDocuments(IntPoint.newExactQuery("id", 1)); //用intPoint可以删除
		//iw.deleteDocuments(new Term("creator","张三丰")); //这样就可以删除creator为张三丰的记录
		iw.close();
		IndexSearcherDemo.searchDoc(indexPath, "title","百度", 100);
	}
	
	public static void main(String[] args) {
		String indexDir = "D:\\nas\\samp\\lucene\\test01\\index";
		try {
			indexDeletedByWriter(indexDir);
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}
